import org.geotools.factory.CommonFactoryFinder;
import org.geotools.filter.text.ecql.ECQL;
import org.geotools.styling.Stroke;
import org.geotools.styling.*;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @Author jlj
 * @Date 2020/12/1 14:35
 * @Description 创建sld样式文件[V1.0.0版本的形式]
 * @Modified
 */
public class CreateSLD {
//    static {
//        Hints.putSystemDefault(Hints.STYLE_FACTORY, 1.1);
////        GeoTools.getDefaultHints().put(Hints.STYLE_FACTORY,new String("1.1.0"));
//    }
    //基础的成员变量，实际使用时既要设置常量默认值，也要能接收外接传入的参数
    private static final Color Line_Color = Color.blue;
    private static final Color Fill_Color = Color.yellow;
    private static final float Opacity = 1.0f;//透明度
    private static final float Line_Width = 1.5f;
    private static final float Point_Size = 10.0f;
    //sld成员，用于生成标签元素、expression和filter
    private StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
    private FilterFactory2 filterFactory2 = CommonFactoryFinder.getFilterFactory2();
    private SLDTransformer sldTransformer = new SLDTransformer();
    //生成一个根节点
    FeatureTypeStyle ftsRoot = styleFactory.createFeatureTypeStyle();

    public void createSLD_Point(String pathTest) {
        try {


//            System.out.println(Hints.STYLE_FACTORY.getValueClass().toString());
//                        if (Hints.STYLE_FACTORY.isCompatibleValue(String.valueOf(1.1))) {
//                System.out.println("成功");
//            }else {
//                System.out.println("失败");
//            }
        } catch (Exception error) {
            System.out.println("生成样式[*.sld]00文件失败：" + error.getLocalizedMessage());
        }
    }

    public void createSLD_Polygon(String geometryPropName, String pathSLD) {
        try {
            //添加边线、填充，加入到FeatureTypeStyle下面(literal:字面意义的)
            Stroke stroke = styleFactory.createStroke(filterFactory2.literal(Line_Color),
                    filterFactory2.literal(Line_Width), filterFactory2.literal(Opacity));
            Fill fill = styleFactory.createFill(filterFactory2.literal(Fill_Color), filterFactory2.literal(Opacity));
            Symbolizer symbolizer = styleFactory.createPolygonSymbolizer(stroke, fill, geometryPropName);
            Rule rule = styleFactory.createRule();
            rule.setName("自定义面样式");
            rule.symbolizers().add(symbolizer);
            //添加过滤条件，可以通过它给WMS图片添加唯一值渲染,加入到相应的rule中
            Filter filter = ECQL.toFilter("Layer='A'");
            rule.setFilter(filter);
            //添加当前rule
            ftsRoot.rules().add(rule);
            //生成style，对应的UserStyle标签
            Style userStyle = styleFactory.createStyle();
            userStyle.setName("myStyle_Name_Polygon");
            userStyle.featureTypeStyles().add(ftsRoot);
            NamedLayer namedLayer=new NamedLayerImpl();
            namedLayer.addStyle(userStyle);
            //生成StyledLayerDescription，顶级sld的根节点
            StyledLayerDescriptor sld = new StyledLayerDescriptorImpl();
//            UserLayer userLayer = new UserLayerImpl();
//            userLayer.setUserStyles(new Style[]{userStyle});
//            sld.addStyledLayer(userLayer);
            sld.addStyledLayer(namedLayer);
            //最后产生样式文件
            File fileSLD = new File(pathSLD);
            OutputStream out = new FileOutputStream(fileSLD);
            sldTransformer.transform(sld, out);
            System.out.println("转换结果："+sldTransformer.transform(sld));
            out.flush();
            out.close();
            System.out.println("生成结果是：" + pathSLD);
        } catch (Exception error) {
            System.out.println("生成样式[*.sld]文件失败：" + error.getLocalizedMessage());
        }
    }
}

